import { get, post, patch, del } from "./fetch";

/**
 * 获取课程套餐列表
 * @param {Object} params 查询参数
 * @returns {Promise<Array>} 课程套餐列表
 */
export const getCoursePackages = async ({
  client_id,
  coach_id,
  branch_campus_id,
  course_type,
  status,
  channel,
}) => {
  'use server'
  try {
    const coursePackages = await get("/api/v1/course-packages", {
      client_id,
      coach_id,
      branch_campus_id,
      course_type,
      status,
      channel,
    });
    return coursePackages;
  } catch (error) {
    throw error;
  }
};

/**
 * 创建课程套餐
 * @param {Object} params 课程套餐信息
 * @returns {Promise<Object>} 创建的课程套餐
 */
export const createCoursePackage = async ({
  client_id,
  course_type,
  total_count,
  left_count,
  price,
  branch_campus_id,
  coach_id,
  sales_commission_rate,
  channel,
  status,
  memo,
}) => {
  try {
    const newCoursePackage = await post("/api/v1/course-packages", {
      client_id,
      course_type,
      total_count,
      left_count,
      price,
      branch_campus_id,
      coach_id,
      sales_commission_rate,
      channel,
      status,
      memo,
    });
    return newCoursePackage;
  } catch (error) {
    throw error;
  }
};

/**
 * 更新课程套餐状态
 * @param {Object} params 课程套餐信息
 * @returns {Promise<Object>} 更新的课程套餐
 */
export const updateCoursePackageStatus = async ({ id, status, memo, client_id, course_type, total_count, left_count, price, branch_campus_id, coach_id, sales_commission_rate, channel }) => {
  try {
    const updatedCoursePackage = await patch(`/api/v1/course-packages/${id}`, { status, memo, client_id, course_type, total_count, left_count, price, branch_campus_id, coach_id, sales_commission_rate, channel });
    return updatedCoursePackage;
  } catch (error) {
    throw error;
  }
}

/**
 * 获取客户的课程套餐列表
 * @param {string} clientId 客户ID
 * @returns {Promise<Array>} 课程套餐列表
 */
export async function getClientCoursePackages(
  clientId,
  { status, course_type, branch_campus_id }
) {
  try {
    const coursePackages = await get(
      `/api/v1/clients/${clientId}/course-packages`,
      { status, course_type, branch_campus_id }
    );
    return coursePackages;
  } catch (error) {
    throw error;
  }
}

/**
 * 扣除课程套餐课时
 * @param {string} id 课程套餐ID
 * @param {Object} params 扣除课时信息
 * @returns {Promise<Object>} 扣除课时后的课程套餐
 */
export async function deductCoursePackageClasses(id, { count }) {
  try {
    const deductedCoursePackage = await post(
      `/api/v1/course-packages/${id}/deduct`,
      { count }
    );
    return deductedCoursePackage;
  } catch (error) {
    throw error;
  }
}

/**
 * 删除课程套餐
 * @param {string} id 课程套餐ID
 */
export async function deleteCoursePackage(id) {
  try {
    await del(`/api/v1/course-packages/${id}`);
  } catch (error) {
    throw error;
  }
}

/**
 * 获取客户的课程套餐统计信息
 * @param {string} clientId 客户ID
 * @returns {Promise<Object>} 课程套餐统计信息
 */
export async function getCoursePackageStats(clientId) {
  try {
    const coursePackageStats = await get(
      `/api/v1/clients/${clientId}/course-packages/stats`
    );
    return coursePackageStats;
  } catch (error) {
    throw error;
  }
}

/**
 * 获取教练关联的课程套餐列表
 * @param {string} coachId 教练ID
 * @returns {Promise<Array>} 课程套餐列表
 */
export async function getCoachCoursePackages(
  coachId,
  { status, course_type, branch_campus_id }
) {
  try {
    const coursePackages = await get(
      `/api/v1/coaches/${coachId}/course-packages`,
      { status, course_type, branch_campus_id }
    );
    return coursePackages;
  } catch (error) {
    throw error;
  }
}
